﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using NCP.Recruiting.ApplicationServices.Interfaces;
using NCP.Recruiting.WebApp.Models;
using NCP.Recruiting.WebApp.Models.MessageTemplates;
using NCP.Recruiting.ApplicationServices.Messaging.Settings;
using NCP.Recruiting.ApplicationServices.ViewModels;

namespace NCP.Recruiting.WebApp.Controllers
{
    public class MessageTemplateController : AuthorizedController
    {

        private readonly ICompanyAppService _companyAppService;
        private readonly ISettingAppService _settingAppService;

        public MessageTemplateController(ICompanyAppService companyAppService,
            ISettingAppService settingAppService)
            : base(companyAppService)
        {
            _settingAppService = settingAppService;
            _companyAppService = companyAppService;
        }
        //
        // GET: /MessageTemplate/

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult List(MessageTemplatePagingFilteringModel command)
        {
            var model = new MessageTemplateListModel();

            if (command.PageSize <= 0)
                command.PageSize = 10;
            if (command.PageNumber <= 0)
                command.PageNumber = 1;

            var request = new FindMessageTemplatesRequest
            {
                CompanyId = CurrentUserId,
                PageIndex = command.PageNumber - 1,
                PageCount = command.PageSize
            };


            var response = _settingAppService.FindMessageTemplates(request);
            model.PagingFilteringContext.LoadPagedList(response.PagedMessageTemplates);
            model.MessageTemplates = response.PagedMessageTemplates;

            return View(model);
        }
        public ActionResult Add()
        {
            return View(new MessageTemplateFormModel());
        }

        [HttpPost]
        public ActionResult Add(MessageTemplateFormModel formModel)
        {
            if (ModelState.IsValid)
            {
                var request = new CreateMessageTemplateRequest();
                request.CompanyId = this.CurrentUserId;
                request.Properties = formModel;

                var response = _settingAppService.CreateMessageTemplate(request);

                return RedirectToAction("List");
            }

            return View();
        }

        public ActionResult Edit(Guid id)
        {
            var response = _settingAppService.GetMessageTemplate(new GetMessageTemplateRequest(id));

            return View(ConstructForm(response.Properties));
        }

        [HttpPost]
        public ActionResult Edit(MessageTemplateFormModel formModel)
        {
            if (TryUpdateMessageTemplate(formModel))
            {
                return RedirectToAction("List");
            }

            var fm = ConstructForm(_settingAppService.GetMessageTemplate(new GetMessageTemplateRequest(formModel.Id)).Properties);
            return View(fm);
        }

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(Guid id)
        {
            var request = new DeleteMessageTemplateRequest(id);
            var response = _settingAppService.DeleteMessageTemplate(request);

            if (response.Exception == null)
            {
                RedirectToAction("List");
            }

            return RedirectToAction("List");
        }

        private bool TryUpdateMessageTemplate(MessageTemplateFormModel formModel)
        {
            if (!ModelState.IsValid)
                return false;

            if (ModelState.IsValid)
            {
                var request = new UpdateMessageTemplateRequest(formModel.Id);
                request.Properties = formModel;
                var response = _settingAppService.UpdateMessageTemplate(request);

                if (response.Exception == null)
                    return true;
            }

            return false;
        }

        private MessageTemplateFormModel ConstructForm(MessageTemplateViewModel vmodel)
        {
            return new MessageTemplateFormModel
            {
                Id = vmodel.Id,
                Deleted = vmodel.Deleted,
                Purpose = vmodel.Purpose,
                Subject = vmodel.Subject,
                TemplateContent = vmodel.TemplateContent
            };
        }
    }

}
